package com.qf.web.controller;


import com.qf.entity.*;
import com.qf.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 预约列表管理
 */
@RestController//直接让方法返回Java对象
@RequestMapping("/reservationlist")
public class ReservationController {


    @Autowired
    private ReservationService reservationService;

    /**
     * 通过id查询检查组
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        ReservationList byId = reservationService.findById(id);
        if(byId != null){
            Result result = Result.success(byId);
            return result;
        }
        return Result.error("查询预约失败");
    }

    /**
     * 通过预约id查询套餐id
     * @param id
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try{
            List<Integer> checkitemIds =
                    reservationService.findCheckItemIdsByCheckGroupId(id);
            return Result.success(checkitemIds);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("查询预约失败");
        }
    }
    /**
     * 查询所有套餐
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        System.out.println("查询所有");
        List<SetMeal> all = reservationService.findAll();
        if(all != null && all.size() > 0){
            Result result = Result.success("请选择检查项");
            result.setData(all);
            return result;
        }
        return  Result.error("无检查项,请添加检查项");
    }

    /**
     * 新增预约
     */
    @RequestMapping("/add")
    public Result add(@RequestBody ReservationList reservationList, Integer[] setmealIds){
        System.out.println(111);
        System.out.println(reservationList);
        System.out.println(setmealIds);
        try {
            reservationService.add(reservationList,setmealIds);
        }catch (Exception e){
            //新增失败
            return Result.error("新增预约失败");
        }
        //新增成功
        return Result.success("新增预约成功");
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            reservationService.delete(id);
        }catch (RuntimeException e){
            System.out.println(222);
            return Result.error("取消预约失败");
        }catch (Exception e){
            return Result.error("取消预约失败");
        }
        return Result.success("取消预约成功");
    }


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = reservationService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


}
